//
//  EFSRefreshView.m
//  EFSMobile
//
//  Created by Chuan on 11/27/15.
//  Copyright © 2015 Elephants Financial Service. All rights reserved.
//

#import "EFSRefreshView.h"

#import <AFNetworking/AFNetworking.h>


@interface EFSRefreshView()
{
    NSString *_hintString;
}
@property (nonatomic, strong) UIImageView * animatingView;

@end

@implementation EFSRefreshView

- (UIImageView *)animatingView {
    if (!_animatingView) {
        _animatingView = [UIImageView new];
        _animatingView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _animatingView;
}

- (UILabel *)hintLabel {
    if (!_hintLabel) {
        _hintLabel = [UILabel new];
        _hintLabel.textColor = [UIColor lightGrayColor];
        _hintLabel.font = [UIFont systemFontOfSize:15.f];
        _hintLabel.numberOfLines = 0;
        _hintLabel.textAlignment = NSTextAlignmentCenter;
        _hintLabel.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _hintLabel;
}

- (UIButton *)refreshButton {
    if (!_refreshButton) {
        _refreshButton = [UIButton new];
        [_refreshButton setBackgroundImage:[UIImage imageNamed:@"refresh_button"] forState:UIControlStateNormal];
        _refreshButton.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _refreshButton;
}

- (void)setRefreshButtonTapped:(void (^)(void))refreshButtonTapped {
    _refreshButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        if (refreshButtonTapped) {
            refreshButtonTapped();
        }
        return [RACSignal empty];
    }];
}

#pragma mark - init programmatically
- (instancetype)initWithRefreshStatus:(RefreshStatusType)refreshStatus {
    self = [super init];
    if (self) {
        [self setupUI];
        [self configRefreshStatus:refreshStatus];
    }
    return self;
}

#pragma mark - init in storyboard FOR CELL
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
}

- (void)setupUI {
    [self addSubview:self.animatingView];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.animatingView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.animatingView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:35.0]];
    
    [self addSubview:self.hintLabel];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.hintLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.hintLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.animatingView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.hintLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30.0]];
    
    [self addSubview:self.refreshButton];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.refreshButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.hintLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:5.0]];
}

- (void)configRefreshStatus:(RefreshStatusType)refreshStatus {
    //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    switch (refreshStatus) {
        case RefreshStatusTypeEmpty:
        {
            NSMutableArray *refreshingImages = [NSMutableArray array];
            for (NSUInteger i = 1; i<=10; i++) {
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refreshing0%zd", i]];
                [refreshingImages addObject:image];
            }
            
            _animatingView.animationImages = refreshingImages;//将序列帧数组赋给UIImageView的animationImages属性
            _animatingView.animationDuration = 1;//设置动画时间
            _animatingView.animationRepeatCount = 0;//设置动画次数 0 表示无限
            [_animatingView sizeToFit];
            [_animatingView startAnimating];
            
            _hintLabel.text = _hintString? [_hintString copy] : @"正在加载...";
            [_hintLabel sizeToFit];
            
            _refreshButton.hidden = YES;
            break;
        }
        case RefreshStatusTypeNoData:
        {
            _animatingView.animationImages = nil;//将序列帧数组赋给UIImageView的animationImages属性
            [_animatingView stopAnimating];
            
            _animatingView.image = [UIImage imageNamed:@"empty_nodata"];
            [_animatingView sizeToFit];
            
            _hintLabel.text = _hintString? [_hintString copy] : @"暂时没有消息";
            [_hintLabel sizeToFit];
            
            _refreshButton.hidden = NO;
            
            break;
        }
        case RefreshStatusTypeFirstError:
        {
            _animatingView.animationImages = nil;//将序列帧数组赋给UIImageView的animationImages属性
            [_animatingView stopAnimating];
            
            _animatingView.image = [UIImage imageNamed:@"empty_nonetwork"];
            [_animatingView sizeToFit];
            
            _hintLabel.text = @"网络偷懒了，再刷新试试";
            [_hintLabel sizeToFit];
            
            _refreshButton.hidden = NO;
            
            break;
        }
        case RefreshStatusTypeError:
        {
            _animatingView.animationImages = nil;//将序列帧数组赋给UIImageView的animationImages属性
            [_animatingView stopAnimating];
            
            _animatingView.image = [UIImage imageNamed:@"empty_nonetwork"];
            [_animatingView sizeToFit];
             BOOL reachable = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
            _hintLabel.text = reachable?@"服务器开了点小差，请稍后再试":@"网络不给力，请稍后再试";
            [_hintLabel sizeToFit];
            
            _refreshButton.hidden = NO;
            
            break;
        }
    }
    _hintString = nil;
}

- (void)configRefreshStatus:(RefreshStatusType)refreshStatus withHintString:(NSString *)hintString
{
    _hintString = hintString;
    [self configRefreshStatus:refreshStatus];
}

#pragma mark - common methods

- (void)stopImageAnimating {
    [_animatingView stopAnimating];
}

@end
